package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.adapter.AllCommentsAdapter;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.AllCommentsBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.PraiseBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 11.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 *
 * @desc 全部回复
 */
public class AllCommentsActivity extends Activity {


    private static final String TAG ="AllCommentsActivity" ;
    @Bind(R.id.activity_all_comments_iv_return)
    ImageView activityAllCommentsIvReturn;
    @Bind(R.id.activity_all_comments_iv_head_portrait)
    ImageView activityAllCommentsIvHeadPortrait;
    @Bind(R.id.activity_all_comments_tv_name)
    TextView activityAllCommentsTvName;
    @Bind(R.id.activity_all_comments_tv_date)
    TextView activityAllCommentsTvDate;

    @Bind(R.id.activity_all_comments_tv_content)
    TextView activityAllCommentsTvContent;
    @Bind(R.id.activity_all_comments_recyclerView)
    RecyclerView activityAllCommentsRecyclerView;

    @Bind(R.id.activity_all_comments_et_revert_content)
    EditText activityAllCommentsEtRevertContent;
    @Bind(R.id.activity_all_comments_bt_sand)
    Button activityAllCommentsBtSand;


    private AllCommentsAdapter allCommentsAdapter;
    private String addtime;
    private String content;
    private String cus_name;
    private String cus_id;
    private String id;
    private String cus_logo;
    private  int p = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_comments);
        ButterKnife.bind(this);


        Utils.setStatusBar(this, false, false);
        Utils.setStatusTextColor(true, this);
        Intent intent = getIntent();
        addtime = intent.getStringExtra("addtime");
        content = intent.getStringExtra("content");
        cus_logo = intent.getStringExtra("cus_logo");
        cus_name = intent.getStringExtra("cus_name");
        cus_id = intent.getStringExtra("cus_id");
        id = intent.getStringExtra("id");

        activityAllCommentsTvName.setText(cus_name);
        activityAllCommentsTvDate.setText(addtime);
       activityAllCommentsTvContent.setText(content);
       Glide.with(this).load(RBConstants.SERVER_URL+"/Public/Uploads/"+ cus_logo).transform(new GlideCircleTransform(this)).into(activityAllCommentsIvHeadPortrait);


        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        activityAllCommentsRecyclerView.setLayoutManager(layoutManager);

       initAllComments(p);
    }



    @OnClick({R.id.activity_all_comments_iv_return, R.id.activity_all_comments_bt_sand})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_all_comments_iv_return:
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                break;
            case R.id.activity_all_comments_bt_sand:
                String etRevertContent =  activityAllCommentsEtRevertContent.getText().toString().trim();
                if(!TextUtils.isEmpty(etRevertContent)){
                    // TODO 回复接口 参数有问题 id
                    submitrevertDate(id,cus_id,etRevertContent);
                }else{
                    Toast.makeText(AllCommentsActivity.this,"回复内容不能为空",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    /**
     * @回复接口
     * @param post_comments_id1
     * @param po_id_01*/
    private void submitrevertDate(String post_comments_id1, String po_id_01,String etRevertContent) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String  user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.COMMENTS)
                .addParams("parent",post_comments_id1)
                .addParams("po_id", po_id_01)
                .addParams("content", etRevertContent)
                .addParams("cus_id", user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"praiseParseNetworkResponse====8888========>"+praiseParseNetworkResponse);
                        final PraiseBean praiseBean = gson.fromJson(praiseParseNetworkResponse,PraiseBean.class);
                        if(praiseBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(AllCommentsActivity.this,"回复成功",Toast.LENGTH_SHORT).show();
                                    initAllComments(1);
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(AllCommentsActivity.this,praiseBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 所有回复
     * */
    private void initAllComments(int p) {
        Log.e(TAG,"initAllComments============>");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.ALL_COMMENTS)
                .addParams("com_id", id)
                .addParams("p", p+"")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        final AllCommentsBean allCommentsBean = gson.fromJson(praiseParseNetworkResponse,AllCommentsBean.class);
                        if(allCommentsBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    List<AllCommentsBean.DataBean>  mlist = allCommentsBean.data;
                                    if(mlist != null){
                                        allCommentsAdapter = new AllCommentsAdapter(AllCommentsActivity.this,mlist);
                                        activityAllCommentsRecyclerView.setAdapter(allCommentsAdapter);

                                    }


                                }
                            });

                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(AllCommentsActivity.this,allCommentsBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
